package eu.easyedu.dictionary.api.model;

import java.util.HashSet;
import java.util.Set;
import eu.easyedu.dictionary.api.util.StringUtils;
import eu.easyedu.persistence.annotations.Column;
import eu.easyedu.persistence.annotations.Entity;

/**
 * @author hlavki
 * @version 1.0
 * @created 29-IX-2007 20:04:37
 */
@Entity(table = "WORD")
public class Word {

    @Column(name = "WORD_ID", uniqueName = true)
    private Long id;
    @Column(name = "WORD")
    private String word;
    private Set<WordRelationship> relatedWords;
    @Column(name = "LANGUAGE_ID")
    private Language language;
    @Column(name = "WORD_TYPE")
    private WordType type;
    @Column(name = "NOTES")
    private String notes;

    public Word() {
    }

    public Word(Long id) {
        this.id = id;
    }

    public Word(Long id, String word) {
        this.id = id;
        this.word = word;
    }

    public Word(String word, Language language) {
        this.word = word;
        this.language = language;
        this.type = WordType.UNKNOWN;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Language getLanguage() {
        return language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    public Set<WordRelationship> getRelatedWords() {
        return relatedWords;
    }

    public void setRelatedWords(Set<WordRelationship> relatedWords) {
        this.relatedWords = relatedWords;
    }

    public void addRelatedWord(WordRelationship relationship) {
        if (relatedWords == null) {
            relatedWords = new HashSet<WordRelationship>();
        }
        relationship.setWord(this);
        relatedWords.add(relationship);
    }

    public WordType getType() {
        return type;
    }

    public void setType(WordType type) {
        this.type = type;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String toDetailString() {
        return StringUtils.toString("word", this);
    }

    @Override
    public String toString() {
        return word + " [" + language.getCode() + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Word other = (Word) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public boolean exists() {
        return getId() != null && getId() > 0;
    }
}
